var stand : boolean = true;
var isClimbing : boolean = false;
public static var animationJump : boolean = false;

var speed : float = 4;
var runSeconds : float = 10.0;

var standToRight : Texture2D;
var standToLeft : Texture2D;
var jumping01Right : Texture2D;
var jumping02Right : Texture2D;
var jumping01Left : Texture2D;
var jumping02Left : Texture2D;

var runToRight : Texture2D[];
var runToLeft : Texture2D[];
var climb : Texture2D[];

var bg1 : GameObject;
var bg2 : GameObject;
var bg3 : GameObject;
var bg4 : GameObject;

function Start () 
{
	bg3.renderer.material.mainTextureOffset.x = 0.3;
}

function Update () 
{
	var translationH : float = Input.GetAxis("Horizontal") * speed;
	var translationV : float = Input.GetAxis("Vertical") * speed;
	
	translationH *= Time.deltaTime;
	translationV *= Time.deltaTime;
	transform.position.x += translationH;
	
	// Climb
	if(isClimbing)
	{
		transform.position.y += translationV;
		
		if(translationV != 0 | translationH != 0)
		{
			Climb();
		}
		collider.attachedRigidbody.useGravity = false;
		collider.attachedRigidbody.isKinematic = true;
	}
	else
	{
		collider.attachedRigidbody.useGravity = true;
		collider.attachedRigidbody.isKinematic = false;
	}
	
	// Stand
	if(stand && isClimbing == false)
	{
		renderer.material.mainTexture = standToRight;
	}
	
	// Stand
	if(stand == false && isClimbing == false)
	{
		renderer.material.mainTexture = standToLeft;
	}
	
	// Run
	if(translationH > 0 && isClimbing == false)
	{
		RunToRight();
		stand = true;
	}
	
	// Run
	if(translationH < 0 && isClimbing == false)
	{
		RunToLeft();
		stand = false;
	}
	
	// Parallax
	if(translationH > 0)
	{
		bg1.renderer.material.mainTextureOffset.x += 0.04 * Time.deltaTime;
		bg2.renderer.material.mainTextureOffset.x += 0.02 * Time.deltaTime;
		bg3.renderer.material.mainTextureOffset.x += 0.01 * Time.deltaTime;
		bg4.renderer.material.mainTextureOffset.x += 0.005 * Time.deltaTime;
	}
	
	// Parallax
	if(translationH < 0)
	{
		bg1.renderer.material.mainTextureOffset.x -= 0.04 * Time.deltaTime;
		bg2.renderer.material.mainTextureOffset.x -= 0.02 * Time.deltaTime;
		bg3.renderer.material.mainTextureOffset.x -= 0.01 * Time.deltaTime;
		bg4.renderer.material.mainTextureOffset.x -= 0.005 * Time.deltaTime;
	}
	
	// Parallax
	if(translationH == 0)
	{
		bg1.renderer.material.mainTextureOffset.x += 0.02 * Time.deltaTime;
		bg3.renderer.material.mainTextureOffset.x += 0.01 * Time.deltaTime;
	}
	
	// Jump
	if(Jump.canJump)
	{
		if(Input.GetKeyDown(KeyCode.Space))
		{
			rigidbody.AddForce(0,20000 * Time.deltaTime,0);
			animationJump = true;
		}
	}
	
	if(animationJump)
	{		
		if(rigidbody.velocity.y > 0)
		{
			if(stand)
			{
				renderer.material.mainTexture = jumping01Right;
			}
			
			if(stand == false)
			{
				
			}
		}
		
		if(rigidbody.velocity.y < 0)
		{
			if(stand)
			{
				renderer.material.mainTexture = jumping02Right;
			}
			
			if(stand == false)
			{
				
			}
		}
	}
}

function OnCollisionEnter(other : Collision)
{
	if(other.gameObject.tag == "Enemy")
	{
		Destroy(gameObject);
	}
}

function OnTriggerStay(other : Collider)
{
	if(other.gameObject.tag == "Fence")
	{
		isClimbing = true;
	}
}

function OnTriggerExit(other : Collider)
{
	if(other.gameObject.tag == "Fence")
	{
		isClimbing = false;
	}
}

function RunToRight()
{
	var indexRunToRight = Time.time * runSeconds;
	
	indexRunToRight = indexRunToRight % runToRight.Length;
	renderer.material.mainTexture = runToRight[indexRunToRight];
}

function RunToLeft()
{
	var indexRunToLeft = Time.time * runSeconds;
	
	indexRunToLeft = indexRunToLeft % runToLeft.Length;
	renderer.material.mainTexture = runToLeft[indexRunToLeft];
}

function Climb()
{
	var indexClimb = Time.time * runSeconds;
	
	indexClimb = indexClimb % climb.Length;
	renderer.material.mainTexture = climb[indexClimb];
}